package com.huaxin.hxcloud.work;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.huaxin.common.Entity.DataValuesBean;
import com.huaxin.hxcloud.R;

import java.text.DecimalFormat;

/**
 * @author songxf
 * @date 2017/11/28
 * @Desc 指标dapter
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxStationRealDataAdapter extends BaseQuickAdapter<DataValuesBean, BaseViewHolder> {

    private final Resources mResources;

    public HxStationRealDataAdapter(Context context) {
        super(R.layout.item_realdata);
        mResources = context.getResources();
    }

    @Override
    protected void convert(BaseViewHolder helper, DataValuesBean item) {
        TextView valueTv = helper.getView(R.id.value);
        TextView nameTv = helper.getView(R.id.name);
        ImageView img = helper.getView(R.id.img);
        TextView readWriteTv = helper.getView(R.id.read_write_tv);
        RelativeLayout contentImg = helper.getView(R.id.content_img);
        DecimalFormat decimalFormat = new DecimalFormat("#0.00");
        String flagValueStr = null;
        if (item.getDataMold() == 0) { //开关量
            if (!TextUtils.isEmpty(item.getValue())) {
                if (item.getValue().equals("0")) {
                    flagValueStr = "关";
                } else if (item.getValue().equals("1")) {
                    flagValueStr = "开";
                } else if (item.getValue().equals("2")) {
                    flagValueStr = "自动";
                }
                valueTv.setText(flagValueStr);
            } else {
                valueTv.setText("--");
            }
            if ("rw".equals(item.getRw())) {
                readWriteTv.setVisibility(View.VISIBLE);
            } else {
                readWriteTv.setVisibility(View.INVISIBLE);
            }

            contentImg.setBackgroundResource(R.drawable.bg_data12);
            img.setImageDrawable(mResources.getDrawable(R.drawable.icon_data_com2));
            nameTv.setTextColor(mResources.getColor(R.color.color_5077B2));
            valueTv.setTextColor(mResources.getColor(R.color.color_5077B2));
        } else {//模拟量
            Double vl = 0.0d;
            if (!TextUtils.isEmpty(item.getValue())) {
                vl = Double.parseDouble(item.getValue());
                valueTv.setText((decimalFormat.format(vl) + item.getUnit()));
            } else {
                valueTv.setText("--");
            }
            readWriteTv.setText("");
            contentImg.setBackgroundResource(R.drawable.bg_data06);
            img.setImageDrawable(mResources.getDrawable(R.drawable.site_state_li_5));
            nameTv.setTextColor(mResources.getColor(R.color.white));
            valueTv.setTextColor(mResources.getColor(R.color.white));
            readWriteTv.setBackgroundResource(R.drawable.icon_enter);
        }
        nameTv.setText(item.getName());

    }

}
